﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Media.Media3D;
using System.Windows;
using Eniux.Windows.ThreeDEngine.Helper;

namespace Eniux.Windows.ThreeDEngine.Visual3Ds
{
    /// <summary>
    /// 三维图形四边形
    /// </summary>
   public class QuadrilateralVisual3D : HitMeshElement3D
    {
        // A quadrilateral defined by the four corner points.
        // Point4          Point3
        // +---------------+
        // |               |
        // |               |
        // +---------------+
        // Point1          Point2
        // The texture coordinates are
        // (0,0)           (1,0)
        // +---------------+
        // |               |
        // |               |
        // +---------------+
        // (0,1)          (1,1)
        #region 注册依赖属性

        #region Point1

        /// <summary>
        /// Point1属性
        /// </summary>
        public static readonly DependencyProperty Point1Property = DependencyProperty.Register(
            "Point1",
            typeof(Point3D),
            typeof(QuadrilateralVisual3D),
            new UIPropertyMetadata(new Point3D(0, 0, 0), GeometryChanged));

        /// <summary>
        /// 获取或设置第一个3D点
        /// </summary>
        public Point3D Point1
        {
            get { return (Point3D)this.GetValue(Point1Property); }
            set { this.SetValue(Point1Property, value); }
        } 

        #endregion

        #region Point2

        /// <summary>
        /// Point2属性
        /// </summary>
        public static readonly DependencyProperty Point2Property = DependencyProperty.Register(
            "Point2",
            typeof(Point3D),
            typeof(QuadrilateralVisual3D),
            new UIPropertyMetadata(new Point3D(1, 0, 0), GeometryChanged));

        /// <summary>
        /// 获取或设置第二个3D点
        /// </summary>
        public Point3D Point2
        {
            get { return (Point3D)this.GetValue(Point2Property); }
            set { this.SetValue(Point2Property, value); }
        }

        #endregion

        #region Point3

        /// <summary>
        /// Point3属性
        /// </summary>
        public static readonly DependencyProperty Point3Property = DependencyProperty.Register(
            "Point3",
            typeof(Point3D),
            typeof(QuadrilateralVisual3D),
            new UIPropertyMetadata(new Point3D(1, 1, 0), GeometryChanged));

        /// <summary>
        /// 获取或设置第三个3D点
        /// </summary>
        public Point3D Point3
        {
            get { return (Point3D)this.GetValue(Point3Property); }
            set { this.SetValue(Point3Property, value); }
        } 

        #endregion

        #region Point4

        /// <summary>
        /// Point4属性
        /// </summary>
        public static readonly DependencyProperty Point4Property = DependencyProperty.Register(
            "Point4",
            typeof(Point3D),
            typeof(QuadrilateralVisual3D),
            new UIPropertyMetadata(new Point3D(0, 1, 0), GeometryChanged));

        /// <summary>
        /// 获取或设置第四个3D点
        /// </summary>
        public Point3D Point4
        {
            get { return (Point3D)this.GetValue(Point4Property); }
            set { this.SetValue(Point4Property, value); }
        } 

        #endregion

        #endregion

        #region 重写方法

        protected override MeshGeometry3D Tessellate()
        {
            var builder = new MeshBuilder();
            builder.AddQuad(
                this.Point1,
                this.Point2,
                this.Point3,
                this.Point4,
                new Point(0, 1),
                new Point(1, 1),
                new Point(1, 0),
                new Point(0, 0));
            return builder.ToMesh();
        }

        #endregion
    }
}
